//
//  MineViewController.m
//  FreeTravel
//
//  Created by 徐康 on 16/8/26.
#import "UIImageView+WebCache.h"
#import "UIImage+Common.h"
#import "SetupViewController.h"
#import "MineButton.h"
#import "MineModel.h"
#import "MineCell.h"
#import "FreeTravelMacro.h"
#import "Masonry.h"
#import "MineViewController.h"
#import "LoginViewController.h"
#import "PhoneLoginViewController.h"
#import "MyCollectionOfDestinationViewController.h"
@interface MineViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
// 本地的图片数组
@property (nonatomic, strong) NSArray *arrOfImage;
// 本地数据
@property (nonatomic, strong) NSArray *arrOfData;
// 头视图
@property (nonatomic, strong) UIView *backViewForTableHeaderView;
// 添加到bar上的view
@property (nonatomic, strong) UIView *barImageView;
// 头视图的背景图片
@property (nonatomic, strong) UIImageView *backImageViewForTableHeaderView;
// model数组
@property (nonatomic, strong) NSMutableArray *arrOfModel;
// 存放区的数组
@property (nonatomic, strong) NSMutableArray *arrOfSection;
// 头像
@property (nonatomic, strong) UIImageView *headButtonImageView;
// 昵称
@property (nonatomic, strong) UILabel *headButtonLabel;

@property (nonatomic, strong) UILabel *headButtonSmallLabel;
// 整体按钮
@property (nonatomic, strong) UIButton *headButton;
@end

@implementation MineViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    NSLog(@"%d",[[NSUserDefaults standardUserDefaults] boolForKey:@"status"]);
        [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
        [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    [self.navigationController.navigationBar setTranslucent:YES];
    [self.navigationController.navigationBar setBarTintColor:[UIColor clearColor]];
    self.tabBarController.tabBar.hidden = NO;
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"status"]) {
        
            NSString *strOfHeadButtonImageView = [[NSUserDefaults standardUserDefaults] objectForKey:@"avatar"];
        
            [self.headButtonImageView sd_setImageWithURL:[NSURL URLWithString:strOfHeadButtonImageView]];
        
        NSString *strOfBackImageViewForTableHeaderView = [[NSUserDefaults standardUserDefaults] objectForKey:@"cover"];
            [self.backImageViewForTableHeaderView sd_setImageWithURL:[NSURL URLWithString:strOfBackImageViewForTableHeaderView]];
        
        self.headButtonLabel.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"username"];
        
    }else{
        self.headButtonImageView.image = [UIImage imageNamed:@"GroupMember_Cell_defaultAvatar"];
        self.backImageViewForTableHeaderView.image = [UIImage imageNamed:@"profileHeader"];
        self.headButtonLabel.text = @"点击登录,体验更多";
    }
    
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
NSLog(@"%d",[[NSUserDefaults standardUserDefaults] boolForKey:@"status"]);
    
    self.barImageView = [[UIView alloc]init];
    self.barImageView.backgroundColor = COLORFORFREETRAVEL;
    self.barImageView.alpha = 1;
    [self.navigationController.navigationBar addSubview:self.barImageView];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:button];
    button.frame = CGRectMake(100, 100, 100, 100);
    button.backgroundColor = [UIColor colorWithRed:0.11 green:0.68 blue:0.45 alpha:1.00];
    [button addTarget:self action:@selector(gogogo) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"shezhi"] style:UIBarButtonItemStyleDone target:self action:@selector(rightBarButtonItemAction)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    
    [self handleData];
    [self createTableView];
}
- (void)handleData{
    
    self.arrOfSection = [NSMutableArray array];
    self.arrOfData = @[@[@"我的订单",@"我收藏的折扣",@"我的优惠券"],@[@"我收藏的目的地",@"我的足迹",@"等我点评的目的地"],@[@"我发布的帖子",@"我的问答",@"我的结伴"]];
    self.arrOfImage = @[@[@"dingdan",@"zhekou",@"youhuiquan"],@[@"mudidi",@"zuji",@"dianping"],@[@"tiezi",@"wenda",@"jieban"]];
    for (int i = 0; i < 3; i++) {
        NSArray *array = self.arrOfData[i];
        NSArray *arr = self.arrOfImage[i];
        self.arrOfModel = [NSMutableArray array];
        for (int j = 0; j < 3; j++) {
            MineModel *model = [[MineModel alloc]init];
            model.name = array[j];
            model.stringOfImage = arr[j];
            [self.arrOfModel addObject:model];
        }
        [self.arrOfSection addObject:self.arrOfModel];
    }
    
    
}
- (void)createTableView{
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    [self.view addSubview:self.tableView];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[MineCell class] forCellReuseIdentifier:@"MineViewControllerCell"];
    self.tableView.tableHeaderView = [self createTableHeaderView];
}
#pragma mark - 头视图
- (UIView *)createTableHeaderView{
    self.backViewForTableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 300)];
    self.backImageViewForTableHeaderView = [[UIImageView alloc]initWithFrame:self.backViewForTableHeaderView.bounds];
    
    [self.backViewForTableHeaderView addSubview:self.backImageViewForTableHeaderView];
    // 毛玻璃
    UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *visualView = [[UIVisualEffectView alloc]initWithEffect:effect];
    visualView.frame = CGRectMake(0, 200, SCREEN_WIDTH, 100);
    visualView.alpha = 1;
    [self.backViewForTableHeaderView addSubview:visualView];
    MineButton *firstButton = [[MineButton alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH/3, 100) WithTitle:@"收藏的帖子" image:[UIImage imageNamed:@"shoucangdetiezi"]];
    MineButton *secondButton = [[MineButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/3, 0, SCREEN_WIDTH/3, 100) WithTitle:@"我的锦囊" image:[UIImage imageNamed:@"jinnang"]];
    MineButton *thirdButton = [[MineButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/3*2, 0, SCREEN_WIDTH/3, 100) WithTitle:@"我的行程" image:[UIImage imageNamed:@"xingcheng"]];
    [firstButton addTarget:self action:@selector(goMyCollectionOfDestinationViewController) forControlEvents:UIControlEventTouchUpInside];
    [visualView addSubview:firstButton];
    [visualView addSubview:secondButton];
    [visualView addSubview:thirdButton];
    self.headButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.headButton.frame = CGRectMake(0, 64, SCREEN_WIDTH, 136);
    [self.backViewForTableHeaderView addSubview:_headButton];
    
    self.headButtonSmallLabel = [[UILabel alloc]init];
    self.headButtonImageView = [[UIImageView alloc]init];
    self.headButtonLabel = [[UILabel alloc]init];
    
    
    self.headButtonSmallLabel.textColor = [UIColor whiteColor];
    self.headButtonLabel.textColor = [UIColor whiteColor];
    self.headButtonImageView.frame = CGRectMake(30, (200-64-60)/2, 60, 60);
    self.headButtonLabel.frame = CGRectMake(100, (200-64-60)/2, SCREEN_WIDTH-80, 30);
    self.headButtonSmallLabel.frame = CGRectMake(100, (200-64-60)/2+30, SCREEN_WIDTH-80, 30  );
    // 头像圆角
    self.headButtonImageView.layer.cornerRadius = 30;
    self.headButtonImageView.layer.masksToBounds = YES;
    [self.headButton addSubview:self.headButtonSmallLabel];
    [self.headButton addSubview:self.headButtonLabel];
    [self.headButton addSubview:self.headButtonImageView];
    [self.headButton addTarget:self action:@selector(gogogo) forControlEvents:UIControlEventTouchUpInside];
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"status"]) {
        
        NSString *strOfHeadButtonImageView = [[NSUserDefaults standardUserDefaults] objectForKey:@"avatar"];
        [self.headButtonImageView sd_setImageWithURL:[NSURL URLWithString:strOfHeadButtonImageView]];
        
        NSString *strOfBackImageViewForTableHeaderView = [[NSUserDefaults standardUserDefaults] objectForKey:@"cover"];
        [self.backImageViewForTableHeaderView sd_setImageWithURL:[NSURL URLWithString:strOfBackImageViewForTableHeaderView]];
        
        self.headButtonLabel.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"username"];
        
    }else{
        self.headButtonImageView.image = [UIImage imageNamed:@"GroupMember_Cell_defaultAvatar"];
        self.backImageViewForTableHeaderView.image = [UIImage imageNamed:@"profileHeader"];
        self.headButtonLabel.text = @"点击登录,体验更多";
    }
    return self.backViewForTableHeaderView;
}
#pragma mark - 协议方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MineCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MineViewControllerCell"];
    MineModel *model = self.arrOfSection[indexPath.section][indexPath.row];
    cell.model = model;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)goMyCollectionOfDestinationViewController{
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"status"]) {
        
        MyCollectionOfDestinationViewController *vc  = [[MyCollectionOfDestinationViewController alloc]init];
        vc.userID = [[self.dicOfResult objectForKey:@"data"] objectForKey:@"user_id"];
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        NSLog(@"未登录");
    }
}


// 跳转到登录界面
- (void)gogogo{
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"status"]) {
        
        NSLog(@"用户已登录");
    }else{
        
    
    PhoneLoginViewController *vc = [[PhoneLoginViewController alloc]init];
    UINavigationController *naviOfLogin = [[UINavigationController alloc]initWithRootViewController:vc];
    
    __weak typeof(self) weakSelf = self;
    vc.passResult= ^(id result){
        weakSelf.dicOfResult = result;
        
    };

    [self presentViewController:naviOfLogin animated:YES completion:^{
            }];
        
    }
}
// 滚动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    

    CGFloat offset_Y = scrollView.contentOffset.y;
    CGFloat alpha = offset_Y  / 64.0f;
    self.barImageView.alpha = alpha;
    if (offset_Y < 0) {
        CGFloat add_height = -offset_Y ;
        CGFloat scale = (300 + add_height) / 300.0f;
        self.backImageViewForTableHeaderView.frame = CGRectMake(-(SCREEN_WIDTH * scale - SCREEN_WIDTH)/2.0f, -add_height, SCREEN_WIDTH *scale, 300+add_height);
    }
    
    
    
}
// 设置
- (void)rightBarButtonItemAction{
    SetupViewController *vc = [[SetupViewController alloc]init];
    vc.userName = [[self.dicOfResult objectForKey:@"data"] objectForKey:@"username"];
    [self.navigationController pushViewController:vc animated:NO];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
